<template>
  <div class="enterprise">
    <el-card>
      <div class="cardone">
        <el-form :inline="true">
          <el-form-item label="企业编号">
            <el-input v-model="form.eid" style="width: 100px" />
          </el-form-item>
          <el-form-item label="企业名称">
            <el-input v-model="form.name" style="width: 149px" />
          </el-form-item>
          <el-form-item label="创建者">
            <el-input v-model="form.username" style="width: 100px" />
          </el-form-item>
          <el-form-item label="状态" v-model="form.status">
            <el-select
              v-model="form.status"
              placeholder="选择状态"
              style="width: 149px"
            >
              <el-option
                v-for="(val, key, index) in statusObj"
                :key="index"
                :label="val"
                :value="key"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-button type="primary" @click="searchClick">搜索</el-button>
          <el-button @click="clearClick">清除</el-button>
          <el-button type="primary" @click="addClick">+新增企业</el-button>
        </el-form>
      </div>
    </el-card>
    <el-card>
      <div class="cardtwo">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column label="序号" class="num" prop="id">
            <template v-slot="{ $index }">
              <div>
                {{ (form.page - 1) * form.limit + $index + 1 }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="企业编号" class="number" prop="eid" />
          <el-table-column label="企业名称" prop="name" />
          <el-table-column label="创建者" prop="username" />
          <el-table-column label="状态" prop="status">
            <template v-slot="{ row }">
              {{ row.status ? '启用' : '禁用' }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150">
            <template v-slot="{ row }">
              <div>
                <el-button type="text" @click="addEdit(row)">编辑</el-button>
                <el-button type="text" @click="changeStatus(row)">{{
                  row.status ? '禁用' : '启用'
                }}</el-button>
                <el-button type="text" @click="delData(row.id)">删除</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="block">
        <el-pagination
          layout="total,sizes,prev,pager,next,jumper"
          :total="Number(form.total)"
          :page-sizes="[1, 5, 10, 200, 300]"
          :page-size="Number(form.limit)"
          :current-page="Number(form.page)"
          @size-change="sizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <!-- 新增弹框组件 -->
    <Add ref="add" @getData="getData" />
  </div>
</template>

<script>
import Add from './components/add.vue'
import {
  enterpriseList,
  enterpriseRemove,
  enterpriseStatus
} from '@/api/enterprise.js'
export default {
  components: {
    Add
  },
  data () {
    return {
      isShow: false,
      tableData: [{}],
      // page: {
      //   total: 10,
      //   size: 5,
      //   page: 1
      // },
      check: [],
      form: {
        name: '',
        page: 1,
        eid: '',
        username: '',
        status: '',
        limit: 10,
        total: ''
      },
      statusObj: {
        0: '禁用',
        1: '启动'
      }
    }
  },
  created () {
    this.getData(this.form)
  },
  methods: {
    // 禁用
    changeStatus (row) {
      enterpriseStatus({ id: row.id }).then(() => {
        // 提示一下，设置状态成功，
        this.$message.success('设置状态成功')
        // 刷新数据
        this.getData()
      })
    },
    // 清除功能
    clearClick () {
      this.form.eid = ''
      this.form.name = ''
      this.form.username = ''
      // this.getData()
    },
    // 搜索功能
    searchClick () {
      const res = this.getData(this.form)
      console.log(res)
      // console.log(this.form, 'form')
    },
    async getData () {
      // 调用企业列表数据并渲染
      const res = await enterpriseList(this.form)
      this.tableData = res.data.items
      this.form.total = res.data.pagination.total
      // console.log(res.data.items)
      //   console.log('企业列表信息', res)
    },
    sizeChange (size) {
      this.form.limit = size
      this.form.page = 1
      this.getData()
    },
    pageChange (page) {
      this.form.page = page
      this.getData()
    },
    // 新增点击
    addClick () {
      this.$refs.add.isShow = true
      this.$refs.add.model = 'add'
    },
    // 删除功能
    delData (id) {
      this.$confirm('您确定删除?', '提示')
        .then(async () => {
          await enterpriseRemove(id)
          this.$message.success('删除成功')
          this.getData()
          this.pageChange(1)
        })
        .catch(error => {
          this.$message(error)
        })
    },
    addEdit (row) {
      this.$refs.add.form = JSON.parse(JSON.stringify(row))
      this.$refs.add.isShow = true
      this.$refs.add.model = 'edit'
    }
  }
}
</script>

<style lang="less" scoped>
.enterprise {
  background-color: #e8e9ec;
  .cardone {
    height: 103px;
    padding: 21px 23px 43px;
  }
  .cardtwo {
    padding: 40px 0 30px 23px;
    .num {
      margin-left: 8px;
    }
    .number {
      margin-left: 96px;
    }
  }
  .block {
    text-align: center;
  }
}
</style>
